package hikari.instruments;

import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.gui.panels.BasicObjectPropertiesPanel;
import hikari.gui.panels.ModifyPanel;
import hikari.managers.Manager3D;
import org.swig.jirr.ISceneNode;
import org.swig.jirr.IVideoDriver;
import org.swig.jirr.SMaterial;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
@WiringComponent(singleton = true, lazy = true)
public class ScaleInstrument extends MoveInstrument {

    @Wire
    private Manager3D manager3D;
    @Wire
    private ModifyPanel modifyPanel;

    private ScaleInstrument() {
    }

    @Override
    protected void changeObject(ISceneNode node, char axis, float length) {
        vector3df temp_position = new vector3df(node_state_before_changing);
        length = length / 2.0f;

        switch (axis) {
            case 'x':
                temp_position.addX(length);
                break;
            case 'y':
                temp_position.addY(length);
                break;
            case 'z':
                temp_position.addZ(length);
                break;
        }

        node.setScale(temp_position);
        manager3D.render();
        modifyPanel.updateTransformationData(BasicObjectPropertiesPanel.UPDATE_TYPE.SCALE);
    }

    @Override
    protected void getNodeStateBeforeChangeState(ISceneNode node) {
        node_state_before_changing = new vector3df(node.getScale());
    }

    @Override
    protected void drawMoveArrays(IVideoDriver driver, vector3df position) {
        driver.setMaterial(xColor);
        driver.resetTransformMatrix();

        float axisSizes = getGizmoLength(position);
        vector3df p1 = position.addOperator(new vector3df(axisSizes, 0, 0));
        vector3df p2 = position.addOperator(new vector3df(0, axisSizes, 0));
        vector3df p3 = position.addOperator(new vector3df(0, 0, axisSizes));
        vector3df p1s = position.addOperator(new vector3df(axisSizes / 2, 0, 0));
        vector3df p2s = position.addOperator(new vector3df(0, axisSizes / 2, 0));
        vector3df p3s = position.addOperator(new vector3df(0, 0, axisSizes / 2));

        drawLine(position, p1, selectedAxis == 'x' ? selectedColor : xColor, driver);
        drawLine(position, p2, selectedAxis == 'y' ? selectedColor : yColor, driver);
        drawLine(position, p3, selectedAxis == 'z' ? selectedColor : zColor, driver);
        drawLine(p1s, p2s, selectedColor, driver);
        drawLine(p2s, p3s, selectedColor, driver);
        drawLine(p1s, p3s, selectedColor, driver);
    }

    protected void drawLine(vector3df start, vector3df end, SMaterial material, IVideoDriver driver) {
        driver.setMaterial(material);
        driver.draw3DLine(start, end);
    }

    @Override
    protected void onEndTransformation(ISceneNode node) {
        super.onEndTransformation(node);
    }
}